//The MIT License
//
//Copyright (c) 2009 nodchip
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
package tv.dyndns.kishibe.other;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import tv.dyndns.kishibe.server.DatabaseConnectionPool;

public class AdCreater {
	private static final String TEMPLATE_PREFIX = "<html>\n<head>\n<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n<title>QMAClone ny nodchip(ノドチップ) 広告ユニット</title>\n</head>\n<body>\n<script type=\"text/javascript\"><!--\ngoogle_ad_client = \"pub-2044095449285503\";\ngoogle_ad_width = 728;\ngoogle_ad_height = 90;\ngoogle_ad_format = \"728x90_as\";\ngoogle_ad_type = \"text_image\";\n//2007-04-06: QMAClone\ngoogle_ad_channel = \"9156668885\";\n//-->\n</script>\n<script type=\"text/javascript\" src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">\n</script>\n";
	private static final String TEMPLATE_SURFIX = "</body>\n</html>\n";
	private static final DatabaseConnectionPool connectionPool = new DatabaseConnectionPool();

	public static void main(String[] args) {
		try {
			final StringBuilder builder = new StringBuilder(TEMPLATE_PREFIX);

			final Connection connection = connectionPool.getConnection();
			// final PreparedStatement statement = connection.prepareStatement("SELECT * FROM problem ORDER BY GOOD * BAD % 117 DESC LIMIT 0, 100");
			final PreparedStatement statement = connection.prepareStatement("SELECT * FROM problem WHERE MATCH (SENTENCE, ANSWER0, ANSWER1, ANSWER2, ANSWER3, CHOICE0, CHOICE1, CHOICE2, CHOICE3, NOTE) AGAINST (? IN BOOLEAN MODE)");
			// statement.setString(1, "乱視 遠視 視力回復 レーシック 生命保険 医者 美容 整形 豊胸");
			// データ復旧 保険 乱視 質屋 結婚相談所 遠視 視力回復 レーシック バイク 生命保険 医者 アパート レンタカー 美容 整形 大学入試 豊胸
			// 不動産 金融 消費者金融 クレジット 証券 保険 外貨 取引 投資
			// statement.setString(1, "不動産 金融 消費者金融 クレジット 証券 保険 外貨 取引");
			statement.setString(1, "保険 金融");
			// statement.setString(1, "不動産 証券 保険 外貨 取引 投資");
			// プロテイン コールセンター ダイエット
			// statement.setString(1, "不動産 金融 消費者金融 クレジット 証券 保険 外貨");
			final ResultSet resultSet = statement.executeQuery();
			while (resultSet.next()) {
				final String sentence = resultSet.getString("SENTENCE");
				final String note = resultSet.getString("NOTE");
				final String[] choices = new String[4];
				final String[] answers = new String[4];
				for (int i = 0; i < 4; ++i) {
					choices[i] = resultSet.getString("CHOICE" + i);
					answers[i] = resultSet.getString("ANSWER" + i);
				}

				builder.append("<div>\n");

				builder.append("\t<div>").append(sentence).append("</div>\n");

				builder.append("\t<div>");
				for (String choice : choices) {
					if (choice != null && !choice.isEmpty()) {
						builder.append("<span>").append(choice).append("</span>");
					}
				}
				for (String answer : answers) {
					if (answer != null && !answer.isEmpty()) {
						builder.append("<span>").append(answer).append("</span>");
					}
				}
				builder.append("</div>\n");

				if (note != null && !note.isEmpty()) {
					builder.append("\t<div>").append(note).append("</div>\n");
				}

				builder.append("</div>\n");
			}
			resultSet.close();
			statement.close();
			connection.close();

			builder.append(TEMPLATE_SURFIX);

			final BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("ad.html"));
			outputStream.write(builder.toString().getBytes("UTF-8"));
			outputStream.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
